package com.xiaobai.common;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-16
 **/

@Data
@NoArgsConstructor
public class DelayIntTask implements Delayed {

    public DelayIntTask(Integer id, Date delayTime) {
        this.id = id;
        this.delayTime = delayTime;
    }

    private Integer id;
    private Date delayTime;

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.getDelayTime().getTime()-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return this.getDelayTime().compareTo(((DelayIntTask)o).getDelayTime());
    }

    @Override
    public boolean equals(Object o) {
        return ((DelayIntTask) o).getId().equals(this.getId());
    }

    @Override
    public int hashCode() {
        return id;
    }
}
